<?php
/*
*	稿费统计相关模型
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Paid_m extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->table='associates';
    }
    public function sel($where=array(),$field='*'){
    	$this->com_where($where);
    	$this->db->select($field);
      	$query=$this->db->get($this->table);
     	$data=$query->row_array();
      	return $data;
    }
    //稿件统计
    public function selInfoList($params=array())
    {
        $count_sql = 'SELECT COUNT(c.`infoid`) AS counts FROM (SELECT b.`infoid` FROM `akr_associates` AS a JOIN `akr_info` AS b ON a.`infoid`=b.`infoid` WHERE 1';
        if(isset($params['start_time']))
        {
            $count_sql.=' AND b.`addtime`>='.$params['start_time'];
        }
        if(isset($params['end_time']))
        {
            $count_sql.=' AND b.`addtime`<='.$params['end_time'];
        }
        $count_sql .= ' GROUP BY a.`infoid`) AS c;';
        $query=$this->db->query($count_sql);
        $counts=$query->row_array();
        $field = 'COUNT(A.`id`) AS authnums,A.`infoid`,B.`title` AS infotitle,B.`copyfrom`,B.`addtime`';
        $this->db->select($field);
        $this->db->from($this->table." as A");
        $this->db->join('info as B','B.`infoid`=A.`infoid`');
        if(isset($params['start_time']))
        {
            $this->db->where('B.`addtime` >=',$params['start_time']);
        }
        if(isset($params['end_time']))
        {
            $this->db->where('B.`addtime` <=',$params['end_time']);
        }
        $this->db->group_by('A.`infoid`');
        $pagesize=getConfig('pagesize');
        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$pagesize * ($page - 1);
        $this->db->limit($pagesize,$startRow);
        $query2 = $this->db->get();
        $data['list'] = $query2->result_array();
        foreach ($data['list'] as $k => $v) {
            $data['list'][$k]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $where['infoid'] = array('=', intval($v['infoid']));
            $authToInfo = $this->selAuthToInfo($where, 'A.`infoid`,A.`utype`,A.`studentid`,B.`name` AS studentname');
            $authorinfo = array();
            foreach ($authToInfo as $key => $value) {
                $utype = $value['utype'] == '1' ? '摄影' : '作者';
                $authorinfo[] = $utype . $value['studentname'];
            }
            $data['list'][$k]['authors'] = implode('、', $authorinfo);
        }
        $data['pageshow']=$this->pageajax_show($pagesize,$counts['counts']);
//        echo '<pre>';print_r($data);echo '</pre>';die;
        return $data;
    }
    //查询所有 文章作者关联表和学生表
    public function selAuthToInfo($where = array(), $field = "*")
    {
        $this->com_where($where);
        $this->db->select($field);
        $this->db->from($this->table." as A");
        $this->db->join('student as B','B.`id`=A.`studentid`');
        $query = $this->db->get();
        $data = $query->result_array();
        return $data;
    }
    //学生稿费统计
    public function selAuthMoney($params=array())
    {
        $count_sql = 'SELECT COUNT(c.`studentid`) AS counts FROM (SELECT a.`studentid` FROM `akr_associates` AS a JOIN `akr_student` AS b ON a.`studentid`=b.`id` WHERE b.`status`=1';
        /*if(isset($params['status']))
        {//暂时不考虑，默认全部已支付
            $count_sql.=' AND a.`status`='.$params['status'];
        }*/
        if(isset($params['start_time']))
        {
            $count_sql.=' AND a.`infotime`>='.$params['start_time'];
        }
        if(isset($params['end_time']))
        {
            $count_sql.=' AND a.`infotime`<='.$params['end_time'];
        }
        if(isset($params['keywords']))
        {
            $count_sql.=' AND b.`stuid` LIKE \'%'.$params['keywords'].'%\' OR b.`name` LIKE \'%'.$params['keywords'].'%\'';
        }
        $count_sql .= ' GROUP BY a.`studentid`) AS c;';
        $query=$this->db->query($count_sql);
        $counts=$query->row_array();
        $field = 'COUNT(A.`id`) AS counts,A.`studentid`,B.`name` AS studentname';
        $this->db->select($field);
        $this->db->from($this->table." as A");
        $this->db->join('student as B','B.`id`=A.`studentid`');
        $this->db->where('B.`status`',1);
        /*if(isset($params['status']))
        {//暂时不用，默认全部已支付
            $this->db->where('A.`status`',$params['status']);
        }*/
        if(isset($params['start_time']))
        {
            $this->db->where('A.`infotime` >=',$params['start_time']);
        }
        if(isset($params['end_time']))
        {
            $this->db->where('A.`infotime` <=',$params['end_time']);
        }
        if(isset($params['keywords']))
        {
            $this->db->group_start()->like('B.`stuid`', $params['keywords'])->or_like('B.`name`', $params['keywords'])->group_end();
        }
        $this->db->group_by("A.`studentid`");
        $pagesize=getConfig('pagesize');
        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$pagesize * ($page - 1);
        $this->db->limit($pagesize,$startRow);
        $query2 = $this->db->get();
        $data['list'] = $query2->result_array();
        $price = getConfig('paid');
        foreach ($data['list'] as $k=>$v)
        {
            $data['list'][$k]['totalmoney'] = $price*$v['counts'];
        }
        $data['pageshow']=$this->pageajax_show($pagesize,$counts['counts']);
//        echo '<pre>';print_r($data);echo '</pre>';die;
        return $data;
    }
    //学生发稿详情
    public function selInfoToAuth($params=array())
    {
        if(empty($params['studentid'])){
            return array('list'=>false,'pageshow'=>false);
        }
        $count_sql = 'SELECT COUNT(c.`infoid`) AS counts FROM (
SELECT a.`infoid` FROM `akr_associates` AS a JOIN `akr_info` AS b ON a.`infoid`=b.`infoid` WHERE a.`studentid`='.$params['studentid'];
        if(isset($params['start_time']))
        {
            $count_sql.=' AND b.`addtime`>='.$params['start_time'];
        }
        if(isset($params['end_time']))
        {
            $count_sql.=' AND b.`addtime`<='.$params['end_time'];
        }
        $count_sql.=') AS c;';
        $query=$this->db->query($count_sql);
        $counts=$query->row_array();
        $field = 'A.`infoid`,A.`utype`,A.`studentid`,B.`title` AS infotitle,B.`copyfrom`,B.`addtime`';
        $this->db->select($field);
        $this->db->from($this->table." as A");
        $this->db->join('info as B','B.`infoid`=A.`infoid`');
        $this->db->where('A.`studentid`',$params['studentid']);
        if(isset($params['start_time']))
        {
            $this->db->where('B.`addtime` >=',$params['start_time']);
        }
        if(isset($params['end_time']))
        {
            $this->db->where('B.`addtime` <=',$params['end_time']);
        }
        $this->db->group_by('A.`infoid`');
        $pagesize=getConfig('pagesize');
        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$pagesize * ($page - 1);
        $this->db->limit($pagesize,$startRow);
        $query2 = $this->db->get();
        $data['list'] = $query2->result_array();
        foreach ($data['list'] as $k => $v) {
            $data['list'][$k]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $where['id'] = array('=', intval($v['studentid']));
            $this->db->where('id',intval($v['studentid']));
            $this->db->select('`id`,`name`');
            $res=$this->db->get('student');
            $stuinfo=$res->row_array();
            $utype = $v['utype'] == '1' ? '摄影' : '作者';
            $data['list'][$k]['author']=$utype.':'.$stuinfo['name'];
        }
        $data['pageshow']=$this->pageajax_show($pagesize,$counts['counts']);
//        echo '<pre>';print_r($data);echo '</pre>';die;
        return $data;
    }
 }